using Microsoft.EntityFrameworkCore;
using GroupSix.Domain.Entity.App;

namespace GroupSix.Infrastructure.Data
{
    public class GroupSixDbContext : DbContext
    {

        public GroupSixDbContext(DbContextOptions<GroupSixDbContext> options) : base(options){}

        public DbSet<GsUser> GsUsers { get; set; }
        public DbSet<GsRole> GsRoles { get; set; }
        public DbSet<GsMenu> GsMenus { get; set; }
        public DbSet<GsUserRole> GsUserRoles { get; set; }
        public DbSet<GsRoleMenu> GsRoleMenus { get; set; }
        public DbSet<GsSystemSetting> GsSystemSettings { get; set; }
        public DbSet<GsSystemSettingLog> GsSystemSettingLogs { get; set; }
        public DbSet<GsOperationLog> GsOperationLogs { get; set; }
        public DbSet<GsArticle> GsArticles { get; set; }
        public DbSet<GsArticleCategory> GsArticleCategories { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置GsUser实体
            modelBuilder.Entity<GsUser>(entity =>
            {
                entity.ToTable("gs_users");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Username).IsRequired().HasMaxLength(50);
                entity.Property(e => e.PasswordHash).IsRequired();
                entity.Property(e => e.Email).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Phone).HasMaxLength(20);
                entity.Property(e => e.Nickname).HasMaxLength(50);
                entity.Property(e => e.Avatar).HasMaxLength(500);
                
                // 唯一索引
                entity.HasIndex(e => e.Username).IsUnique();
                entity.HasIndex(e => e.Email).IsUnique();
            });

            // 配置GsRole实体
            modelBuilder.Entity<GsRole>(entity =>
            {
                entity.ToTable("gs_roles");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Description).HasMaxLength(200);
                entity.Property(e => e.Status).IsRequired().HasMaxLength(20).HasDefaultValue("active");
                
                // 唯一索引
                entity.HasIndex(e => e.Name).IsUnique();
            });

            // 配置GsMenu实体
            modelBuilder.Entity<GsMenu>(entity =>
            {
                entity.ToTable("gs_menus");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Menuname).IsRequired().HasMaxLength(50);
                entity.Property(e => e.MenuPath).HasMaxLength(200);
                entity.Property(e => e.MenuIcon).HasMaxLength(50);
                entity.Property(e => e.PermissionKey).HasMaxLength(100);
                entity.Property(e => e.Order).IsRequired();
                
                // 自引用关系（父子菜单）
                entity.HasOne<GsMenu>()
                    .WithMany(m => m.Children)
                    .HasForeignKey(e => e.ParentId)
                    .OnDelete(DeleteBehavior.Restrict);
            });

            // 配置GsUserRole实体（用户角色关联表）
            modelBuilder.Entity<GsUserRole>(entity =>
            {
                entity.ToTable("gs_user_roles");
                entity.HasKey(e => new { e.UserId, e.RoleId });
                
                entity.HasOne(e => e.User)
                    .WithMany(u => u.GsUserRole)
                    .HasForeignKey(e => e.UserId)
                    .OnDelete(DeleteBehavior.Cascade);
                    
                entity.HasOne(e => e.Role)
                    .WithMany(r => r.GsUserRole)
                    .HasForeignKey(e => e.RoleId)
                    .OnDelete(DeleteBehavior.Cascade);
            });

            // 配置GsRoleMenu实体（角色菜单关联表）
            modelBuilder.Entity<GsRoleMenu>(entity =>
            {
                entity.ToTable("gs_role_menus");
                entity.HasKey(e => new { e.RoleId, e.MenuId });
                
                entity.HasOne(e => e.Role)
                    .WithMany(r => r.GsRoleMenus)
                    .HasForeignKey(e => e.RoleId)
                    .OnDelete(DeleteBehavior.Cascade);
                    
                entity.HasOne(e => e.Menu)
                    .WithMany(m => m.GsRoleMenus)
                    .HasForeignKey(e => e.MenuId)
                    .OnDelete(DeleteBehavior.Cascade);
            });

            // 配置GsSystemSetting实体
            modelBuilder.Entity<GsSystemSetting>(entity =>
            {
                entity.ToTable("gs_system_settings");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.SettingKey).IsRequired().HasMaxLength(100);
                entity.Property(e => e.SettingValue).IsRequired();
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.Property(e => e.SettingType).IsRequired().HasMaxLength(20);
                entity.Property(e => e.Group).IsRequired().HasMaxLength(50);
                entity.Property(e => e.SortOrder).IsRequired();
                entity.Property(e => e.IsSystemLevel).IsRequired();
                entity.Property(e => e.IsReadOnly).IsRequired();
                
                // 唯一索引
                entity.HasIndex(e => e.SettingKey).IsUnique();
                entity.HasIndex(e => e.Group);
            });

            // 配置GsSystemSettingLog实体
            modelBuilder.Entity<GsSystemSettingLog>(entity =>
            {
                entity.ToTable("gs_system_setting_logs");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.SettingKey).IsRequired().HasMaxLength(100);
                entity.Property(e => e.OldValue);
                entity.Property(e => e.NewValue);
                entity.Property(e => e.ChangeReason).HasMaxLength(500);
                entity.Property(e => e.OperationType).IsRequired().HasMaxLength(20);
                entity.Property(e => e.OperatorId).IsRequired();
                entity.Property(e => e.OperatorName).IsRequired().HasMaxLength(50);
                entity.Property(e => e.IpAddress).HasMaxLength(50);
                entity.Property(e => e.UserAgent).HasMaxLength(500);
                
                // 索引
                entity.HasIndex(e => e.SettingKey);
                entity.HasIndex(e => e.OperatorId);
                entity.HasIndex(e => e.CreatedAt);
            });

            // 配置GsOperationLog实体
            modelBuilder.Entity<GsOperationLog>(entity =>
            {
                entity.ToTable("gs_operation_logs");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.OperatorId).IsRequired();
                entity.Property(e => e.OperatorName).IsRequired().HasMaxLength(50);
                entity.Property(e => e.OperationType).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Module).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.Property(e => e.Details);
                entity.Property(e => e.Result).IsRequired().HasMaxLength(20);
                entity.Property(e => e.ErrorMessage);
                entity.Property(e => e.IpAddress).HasMaxLength(50);
                entity.Property(e => e.UserAgent).HasMaxLength(500);
                entity.Property(e => e.RequestUrl).HasMaxLength(500);
                entity.Property(e => e.RequestMethod).HasMaxLength(10);
                entity.Property(e => e.RequestParams);
                entity.Property(e => e.ResponseStatusCode);
                entity.Property(e => e.ExecutionTime);
                entity.Property(e => e.OperationTime).IsRequired();
                
                // 索引
                entity.HasIndex(e => e.OperatorId);
                entity.HasIndex(e => e.OperationType);
                entity.HasIndex(e => e.Module);
                entity.HasIndex(e => e.OperationTime);
                entity.HasIndex(e => e.Result);
            });

            // 配置GsArticle实体
            modelBuilder.Entity<GsArticle>(entity =>
            {
                entity.ToTable("gs_articles");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Title).IsRequired().HasMaxLength(200);
                entity.Property(e => e.Content).IsRequired();
                entity.Property(e => e.Summary).HasMaxLength(500);
                entity.Property(e => e.CategoryId).IsRequired();
                entity.Property(e => e.CategoryName).HasMaxLength(100);
                entity.Property(e => e.Tags).HasMaxLength(500);
                entity.Property(e => e.Status).IsRequired().HasMaxLength(20);
                entity.Property(e => e.AuthorId).IsRequired();
                entity.Property(e => e.AuthorName).HasMaxLength(50);
                entity.Property(e => e.CoverImage).HasMaxLength(500);
                entity.Property(e => e.ViewCount).IsRequired();
                entity.Property(e => e.LikeCount).IsRequired();
                entity.Property(e => e.CommentCount).IsRequired();
                entity.Property(e => e.PublishedAt);
                entity.Property(e => e.SeoTitle).HasMaxLength(200);
                entity.Property(e => e.SeoKeywords).HasMaxLength(500);
                entity.Property(e => e.SeoDescription).HasMaxLength(1000);
                entity.Property(e => e.IsTop).IsRequired();
                entity.Property(e => e.IsRecommend).IsRequired();
                entity.Property(e => e.SortWeight).IsRequired();
                
                // 索引
                entity.HasIndex(e => e.CategoryId);
                entity.HasIndex(e => e.AuthorId);
                entity.HasIndex(e => e.Status);
                entity.HasIndex(e => e.PublishedAt);
                entity.HasIndex(e => e.IsTop);
                entity.HasIndex(e => e.IsRecommend);
                entity.HasIndex(e => e.SortWeight);
                entity.HasIndex(e => e.CreatedAt);
            });

            // 配置GsArticleCategory实体
            modelBuilder.Entity<GsArticleCategory>(entity =>
            {
                entity.ToTable("gs_article_categories");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.Property(e => e.ParentId);
                entity.Property(e => e.Level).IsRequired();
                entity.Property(e => e.SortOrder).IsRequired();
                
                // 自引用关系（父子分类）
                entity.HasOne<GsArticleCategory>()
                    .WithMany()
                    .HasForeignKey(e => e.ParentId)
                    .OnDelete(DeleteBehavior.Restrict);
                
                // 索引
                entity.HasIndex(e => e.ParentId);
                entity.HasIndex(e => e.Level);
                entity.HasIndex(e => e.SortOrder);
            });
        }
    }
}